
class Agent(object):
    
    NAME = "RR"
    
    def __init__(self, id, team, settings=None, field_rects=None, field_grid=None, nav_mesh=None, blob=None, **kwargs):
        """ Each agent is initialized at the beginning of each game.
            The first agent (id==0) can use this to set up global variables.
            Note that the properties pertaining to the game field might not be
            given for each game.
        """
        self.id = id
        self.team = team
        self.mesh = nav_mesh
        self.grid = field_grid
        self.settings = settings
        self.goal = None
        self.callsign = '%s-%d'% (('BLU' if team == TEAM_BLUE else 'RED'), id)
        
        # Read the binary blob, we're not using it though
        if blob is not None:
            print "Agent %s received binary blob of %s" % (
               self.callsign, type(pickle.loads(blob.read())))
            # Reset the file so other agents can read it.
            blob.seek(0) 
        
        # Recommended way to share variables between agents.
        if id == 0:
            self.all_agents = self.__class__.all_agents = []
        self.all_agents.append(self)
    
    def observe(self, observation):
        """ Each agent is passed an observation using this function,
            before being asked for an action. You can store either
            the observation object or its properties to use them
            to determine your action. Note that the observation object
            is modified in place.
        """
        self.observation = observation
        self.selected = observation.selected

        if observation.selected:
            print observation
                    
    def action(self):
        """ This function is called every step and should
            return a tuple in the form: (turn, speed, shoot)
        """ 
        obs = self.observation
        
        for x in range(len(self.all_agents)):
            if x == id:
                continue
            else:
                if self.goal is not None and self.all_agents[x].goal is not None and self.goal == self.all_agents[x].goal:
                    self.goal = None
                
                
                
        gotAllPoints = False
          #Check for CPs
        for x in range(0, len(obs.cps)):
            if(obs.cps[x][2] == 0):
                 gotAllPoints = True
                 continue
            else:
                 gotAllPoints = False
                 break
                 
                 
        # Check if agent reached goal.
        if self.goal is not None and point_dist(self.goal, obs.loc) < self.settings.tilesize:
            for x in range(0, len(obs.cps)):
                if(obs.cps[x][2] != 0):
                    self.goal = obs.cps[x][0:2]
            
        # Drive to where the user clicked
        # Clicked is a list of tuples of (x, y, shift_down, is_selected)
        if self.selected and self.observation.clicked:
            self.goal = self.observation.clicked[0][0:2]
        
        #walk to CP that is not ours
        if self.goal is None:
            for x in range(0, len(obs.cps)):
                if(obs.cps[x][2] != 0):
                    self.goal = obs.cps[x][0:2]
       
         
            
       
        
      
            
        #Camp CP
        shoot = False
        #if point_dist(obs.cps[0][0:2], obs.loc ) < self.settings.max_range and point_dist(obs.cps[1][0:2], obs.loc ) < self.settings.max_range:
        if(gotAllPoints):
            # Walk to ammo
            ammopacks = filter(lambda x: x[2] == "Ammo", obs.objects)
            if ammopacks:
                self.goal = ammopacks[0][0:2]
        #shoot enemies
        if (obs.ammo > 0 and gotAllPoints and
            obs.foes and 
            point_dist(obs.foes[0][0:2], obs.loc) < self.settings.max_range and
            not line_intersects_grid(obs.loc, obs.foes[0][0:2], self.grid, self.settings.tilesize)):
            self.goal = obs.cps[random.randint(0,len(obs.cps)-1)][0:2]
            shoot = True

        # Compute path, angle and drive
        if self.goal is not None:
            path = find_path(obs.loc, self.goal, self.mesh, self.grid, self.settings.tilesize)
            if path:
                dx = path[0][0] - obs.loc[0]
                dy = path[0][1] - obs.loc[1]
                turn = angle_fix(math.atan2(dy, dx) - obs.angle)
                if turn > self.settings.max_turn or turn < -self.settings.max_turn:
                    shoot = False
                speed = (dx**2 + dy**2)**0.5
            else:
                turn = 0
                speed = 0
        else:
                turn = 0
                speed = 0
        
        return (turn,speed,shoot)
    
    def getClosestCP():
        shortest_distance = 999999
        best_index = -1
        for i in range(len(obs.cps)):
            dist = point_dist(obs.loc, 
                obs.cps[i][0:2])
            if shortest_distance > dist:
                shortest_distance = dist
                best_index = i
   
        # this should never happen! 
        if best_index < 0:
            return False
       
       
        return obs.cps[best_index]
        
    def debug(self, surface):
        """ Allows the agents to draw on the game UI,
            Refer to the pygame reference to see how you can
            draw on a pygame.surface. The given surface is
            not cleared automatically. Additionally, this
            function will only be called when the renderer is
            active, and it will only be called for the active team.
        """
        import pygame
        # First agent clears the screen
        if self.id == 0:
            surface.fill((0,0,0,0))
        # Selected agents draw their info
        if self.selected:
            if self.goal is not None:
                pygame.draw.line(surface,(0,0,0),self.observation.loc, self.goal)
        
    def finalize(self, interrupted=False):
        """ This function is called after the game ends, 
            either due to time/score limits, or due to an
            interrupt (CTRL+C) by the user. Use it to
            store any learned variables and write logs/reports.
        """
        pass
        
